package com.douqu.game.core.config;

import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.entity.GameObject;
import com.douqu.game.core.factory.ConfigFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;

/**
 * @author wangzhenfei
 *         2017-12-04 14:32
 *         充值配置
 */
public class RechargeConfig extends GameObject{

    public String icon;

    /**
     * 苹果商店对应的ID
     */
    public String asProductId;

    /**1,月卡  2,钻石**/
    public int type;

    public int  rmb;

    /**额外获得**/
    public GoodsData[]  extra;

    /**月卡天数**/
    public int  days;

    /**直接奖励**/
    public GoodsData[] goods;

    public int linkId;

    @Override
    public boolean check()
    {
        if(goods == null)
        {
            LogUtils.error("RechargeConfig check goods is null -> id:" + id + " name:" + name);
            return false;
        }
        if(extra == null)
        {
            LogUtils.error("RechargeConfig check extra is null -> id:" + id + " name:" + name);
            return false;
        }
        return true;
    }


    public int getDiamond()
    {
        int result = 0;
        for (GoodsData gd: goods){
            if (gd.type == SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_ASSETS_VALUE
                    && gd.id == ConfigFactory.ASSET_MONEY_KEY){
                result = gd.value;
            }
        }
        return result;
    }

    @Override
    public String getVariable(String key)
    {
        if("goods".equals(key))
        {
            StringBuffer sb = new StringBuffer();
            for(GoodsData goodsData : goods)
            {
                sb.append(goodsData.getGoodsStr());
                sb.append(",");
            }
            return sb.length() > 0 ? sb.substring(0, sb.length()-1) : "";
        }else if("extra".equals(key))
        {
            StringBuffer sb = new StringBuffer();
            for(GoodsData goodsData : extra)
            {
                sb.append(goodsData.getGoodsStr());
                sb.append(",");
            }
            return sb.length() > 0 ? sb.substring(0, sb.length()-1) : "";
        }
        else
            return super.getVariable(key);
    }

    @Override
    public boolean setVariable(String key, String value)
    {
        if("goods".equals(key))
        {
            if("0".equals(value)){
                goods = new GoodsData[1];
                goods[0] = new GoodsData();
                return true;
            }
            goods = LoadUtils.loadGoodDataToArray(key, value);
            return LoadUtils.checkGoods(goods, this.getClass(), key, id);
        }else if("extra".equals(key)){
            if("0".equals(value)){
                extra = new GoodsData[1];
                extra[0] = new GoodsData();
                return true;
            }
            extra = LoadUtils.loadGoodDataToArray(key, value);
            return LoadUtils.checkGoods(extra, this.getClass(), key, id);
        }
        else
            return super.setVariable(key, value);
    }

    @Override
    public String toString() {
        return "{" +
                ", type=" + type +
                ", rmb=" + rmb +
                ", extra=" + extra +
                ", days=" + days +
                "} " + super.toString();
    }
}
